package com.flectothink.wch.helper;

import static flexolink.sdk.core.bleDeviceSdk.sdklib.ble.BleEEGService.eventScanAIO;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.Log;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodChannel;

public class AIOHelper implements EventChannel.StreamHandler{

    private static final String TAG = "AIOHelper";

    private Activity context;

    private BluetoothGatt gatt;

    ///写入通道
    private BluetoothGattCharacteristic writeCharacteristic = null;
    ///读通道
    private BluetoothGattCharacteristic readCharacteristic = null;

    public EventChannel.EventSink eventSink;

    public MethodChannel methodChannel;

    public ArrayList<BluetoothDevice> deviceList = new ArrayList<BluetoothDevice>();

    public AIOHelper(Activity context) {
        this.context = context;
    }

    @SuppressLint("MissingPermission")
    public BluetoothDevice getBlueDevice(String deviceName) {
        for (int i = 0; i < deviceList.size(); i ++) {
            BluetoothDevice device = deviceList.get(i);
            if (device.getName().equals(deviceName)) {
                return device;
            }
        }
        return null;
    }

    public void clearDevice(){
        if (this.deviceList == null){
            return;
        }

        this.deviceList.clear();
    }

    @SuppressLint("MissingPermission")
    public void addDevice(BluetoothDevice device) {
        if (!device.getName().startsWith("FLEX_AIO")) {
            return;
        }

        boolean isNew = true;
        for (int i = 0; i < deviceList.size(); i ++) {
            BluetoothDevice device1 = deviceList.get(i);
            if (device1.getName().equals(device.getName())) {
                isNew = false;
                break;
            }
        }

        if (isNew) {
            deviceList.add(device);
            Map<Object, Object> map = new HashMap<>();
            map.put("deviceName", device.getName());
            map.put("mac", device.getAddress());
            map.put("isConnected", false);
            map.put("channelName", "scan");
            map.put("code", 101);

            JSONObject json =new JSONObject(map);
            Log.i(TAG, "android scan");
            methodChannel.invokeMethod(eventScanAIO, map);
        }
    }

    public BluetoothGatt getGatt() {
        return gatt;
    }

    public void setGatt(BluetoothGatt gatt) {
        this.gatt = gatt;
    }

    public BluetoothGattCharacteristic getWriteCharacteristic() {
        return writeCharacteristic;
    }

    public void setWriteCharacteristic(BluetoothGattCharacteristic writeCharacteristic) {
        this.writeCharacteristic = writeCharacteristic;
    }

    public BluetoothGattCharacteristic getReadCharacteristic() {
        return readCharacteristic;
    }

    public void setReadCharacteristic(BluetoothGattCharacteristic readCharacteristic) {
        this.readCharacteristic = readCharacteristic;
    }

    @SuppressLint("MissingPermission")
    public void startReadCharacteristic() {
        this.gatt.readCharacteristic(this.readCharacteristic);
    }

    @Override
    public void onListen(Object arguments, EventChannel.EventSink events) {
        this.eventSink = events;
        Log.d(TAG, "收到 flutter 调用");
        if (arguments instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) arguments;
            String action = (String) map.get("action");
            List<Integer> cmdList = (ArrayList<Integer>) map.get("data");

            if (action.equals("aio")) {
                byte[] cmdArr = new byte[cmdList.size()];
                for (int i = 0; i < cmdList.size(); i++) {
                    cmdArr[i] = cmdList.get(i).byteValue();
                }
            }
        }
    }

    @Override
    public void onCancel(Object arguments) {
    }

}
